package com.glory.wxmemsicpda.activity

import android.annotation.SuppressLint
import android.view.KeyEvent
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.fragment.IndexFragment
import com.glory.wxmemsicpda.fragment.MineFragment
import com.glory.wxmemsicpda.fragment.SearchFragment
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabEntity
import com.pda.platform.ui.ui_pdaplatform.tablayout.listener.CustomTabEntity
import kotlinx.android.synthetic.main.activity_home.*
import java.util.*
import kotlin.system.exitProcess

class HomeActivity : FreeUI_BaseActivity<Any?>() {
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private val mFragments = ArrayList<Fragment>()
    private var exitTime: Long = 0
    override fun getLayoutResId(): Int {
        return R.layout.activity_home
    }

    override fun initView() {}

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun initData() {
        mFragments.add(IndexFragment())
        mFragments.add(SearchFragment())
        mFragments.add(MineFragment())

        val resources = resources
        mTabEntities.add(
            FreeUI_TabEntity(
                "功能管理",
                resources.getDrawable(R.drawable.aos_home_select),
                resources.getDrawable(R.drawable.aos_home_no_select)
            )
        )
        mTabEntities.add(
            FreeUI_TabEntity(
                "信息查询",
                resources.getDrawable(R.drawable.aos_search_select),
                resources.getDrawable(R.drawable.aos_search_unselect)
            )
        )
        mTabEntities.add(
            FreeUI_TabEntity(
                "个人信息",
                resources.getDrawable(R.drawable.aos_mine_select),
                resources.getDrawable(R.drawable.aos_mine_no_select)
            )
        )
        tabLayout.setTabData(mTabEntities, this, R.id.content, mFragments)
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return null
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(applicationContext, "再按一次退出程序", Toast.LENGTH_SHORT).show()
                exitTime = System.currentTimeMillis()
            } else {
                finish()
                exitProcess(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}